#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<unistd.h> 
#include<pthread.h>

int main(int argc, char* args[])
{	
	int pid, status, i, tmpid;
	for(i = 1; i <= 5; i ++)
	{
		pid = fork();
		if(pid < 0){
			perror("fork error");
			exit(1);
		}
		if(pid){
			sleep(1);
			if(i == 2) tmpid = pid;

		}
		else if(!pid) break;
	}

	if(!pid){
		printf("this is the %dth child,pid is %d, ppid is %d\n", i, getpid(),getppid());
		return i;
	}
	else{
		int wpid = waitpid(tmpid, &status, WNOHANG);
		if(wpid == -1){
			perror("waitpid error");
			exit(1);
		}
		else{
			printf("waitpid 2th is %d\n", wpid);
		}
		
	}
	exit(0);
}
